<?php
ob_start();
$headings = array ('id','complete','username','lastname','firstname','middlename',
									 'Address', 'email',
									 'minority_stat', 'minority_info', 'uscitizen_stat', 'uscitizen_info',
									 'disabled_stat', 'disabled_info', 'clin_program','clin_w/o_train',
									 'area_1','area_2','area_3','area_4','res_area_1','res_area_2','res_area_3',
									 'res_area_4','faculty_1','faculty_2','faculty_3','faculty_4', 'verb','verbPercent',
									 'is_new_verb', 'quan', 'quanPercent', 'is_new_quan', 'analy', 'analyPercent', 
									 'is_new_analy', 'ug_gpa', 'grad_gpa', 'highest_degree');

$num = count($headings);
$count = 0;
foreach ($headings as $heading) {
	$count++;
	echo '"' . $heading . '"';
	if ($count < $num) {
		echo "\t";
	}
}
echo "\n";
			 
foreach ($this->data as $app) {
	echo '"' . trim($app['Application']['id']) . '"' . "\t";
	echo '"' . trim($app['Application']['complete']) . '"' . "\t";
	echo '"' . trim($app['User']['username']) . '"' . "\t";
	echo '"' . trim($app['Application']['lastname']) . '"' . "\t";
	echo '"' . trim($app['Application']['firstname']) . '"' . "\t";
	echo '"' . trim($app['Application']['middlename']) . '"' . "\t";
	$address = str_replace("\n", ", ", trim($app['Application']['address']));
	$address = str_replace("\r", "", $address);
	echo '"' . $address . '"' . "\t";
	echo '"' . trim($app['User']['email']) . '"' . "\t";
	echo '"' . trim($app['Application']['minority_stat']) . '"' . "\t";
	echo '"' . trim($app['Application']['minority_info']) . '"' . "\t";
	echo '"' . trim($app['Application']['uscitizen_stat']) . '"' . "\t";
	echo '"' . trim($app['Application']['uscitizen_info']) . '"' . "\t";
	echo '"' . trim($app['Application']['disabled_stat']) . '"' . "\t";
	echo '"' . trim($app['Application']['disabled_info']) . '"' . "\t";
	echo '"' . trim($app['Studyinterest']['is_clinical_app']) . '"' . "\t";
	echo '"' . trim($app['Studyinterest']['non_clinical_interest']) . '"' . "\t";
	echo '"' . trim($app['Studyinterest']['area_1']) . '"' . "\t";
	echo '"' . trim($app['Studyinterest']['area_2']) . '"' . "\t";
	echo '"' . trim($app['Studyinterest']['area_3']) . '"' . "\t";
	echo '"' . trim($app['Studyinterest']['area_4']) . '"' . "\t";
	echo '"' . trim($app['Studyinterest']['res_area_1']) . '"' . "\t";
	echo '"' . trim($app['Studyinterest']['res_area_2']) . '"' . "\t";
	echo '"' . trim($app['Studyinterest']['res_area_3']) . '"' . "\t";
	echo '"' . trim($app['Studyinterest']['res_area_4']) . '"' . "\t";
	echo '"' . trim($app['Studyinterest']['faculty_1']) . '"' . "\t";
	echo '"' . trim($app['Studyinterest']['faculty_2']) . '"' . "\t";
	echo '"' . trim($app['Studyinterest']['faculty_3']) . '"' . "\t";
	echo '"' . trim($app['Studyinterest']['faculty_4']) . '"' . "\t";
	echo '"' . trim($app['Score']['gre_verbal']) . '"' . "\t";
	echo '"' . trim($app['Score']['gre_verbal_p']) . '"' . "\t";
	echo '"' . trim($app['Score']['is_new_verbal']) . '"' . "\t";
	echo '"' . trim($app['Score']['gre_quant']) . '"' . "\t";
  echo '"' . trim($app['Score']['gre_quant_p']) . '"' . "\t";
  echo '"' . trim($app['Score']['is_new_quant']) . '"' . "\t";
	echo '"' . trim($app['Score']['gre_analytical']) . '"' . "\t";
	echo '"' . trim($app['Score']['gre_analytical_p']) . '"' . "\t";
	echo '"' . trim($app['Score']['is_new_analytical']) . '"' . "\t";
	echo '"' . trim($app['Courselist']['cgpa_all']) . '"' . "\t";
	echo '"' . trim($app['Courselist']['cgpa_grad']) . '"' . "\t";

$hd = null;
foreach ($app['School'] as $sch) {
	
	$patt= '/(MD|M\.D|Docto|justi|psyD)/i';
	if (preg_match($patt, trim($sch['degrees']))) {
		$hd = 'P';
	}

	if ($hd != 'P') {
		$patt= '/(JD|J\.D)/i';
		if (preg_match($patt, trim($sch['degrees']))) {
			$hd = 'J';
		}
	}	

	if ($hd != 'P' && $hd != 'J') {
		$patt= '/(MS|MA|ME|M\.A|M\.S|M\.E|Maste)/i';
		if (preg_match($patt, trim($sch['degrees']))) {
			$hd = 'M';
		}
	}	
	
	if ($hd != 'M' && $hd != 'P' && $hd != 'J') {
		$patt= '/(AB|A\.B|BS|BA|BE|B\.A|B\.S|B\.E|Bachelo)/i';
		if (preg_match($patt, trim($sch['degrees']))) {
			$hd = 'B';
		}
	}
	
	if ($hd != 'B' && $hd != 'M' && $hd != 'P' && $hd != 'J') {
		$patt= '/(AA|AS|A\.A|Associ)/i';
		if (preg_match($patt, trim($sch['degrees']))) {
			$hd = 'A';
		}
	}
	
} //end foreach school
	echo '"' . $hd . '"';
	echo "\n";
}
$output= ob_get_contents();
ob_end_clean();
header("Content-type: text/plain");
header("Content-Disposition: attachment; filename=psy_PhD_export.txt");
echo $output;
?>
